﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
#include <QtDebug>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->hCaptureDevice1 = 0;
    this->hCaptureDevice2 = 0;

    this->deviceInitCtrl();
    this->previewInitCtrl();
    this->recordVideoInitCtrl();
    this->layerOverlayInitCtrl();
    this->autoWinInitCtrl();


}

MainWindow::~MainWindow()
{
    if (this->hCaptureDevice1 != 0) {
         AVerStopStreaming(this->hCaptureDevice1);
         AVerDeleteCaptureObject(&this->hCaptureDevice1);

    }
    if (this->hCaptureDevice2 != 0) {
        AVerStopStreaming(this->hCaptureDevice2);
         AVerDeleteCaptureObject(&this->hCaptureDevice2);

    }
    delete ui;
}

void MainWindow::deviceInitCtrl()
{
    // 获取设备数量并初始化
    connect(this->ui->getAVerList, &QPushButton::clicked, this, [=](){
        if (AVerInitialize() != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Capture SDK Intialize Error");
        }

        ui->AVerList->clear();
        DWORD dwNum = 0;
        wchar_t wszName[260] = {L'\0'};
        char szName[260] = {'\0'};
        char *pszName = szName;
        AVerGetDeviceNum(&dwNum);
        if (dwNum == 0)
            return;
        for(int i=0; i<dwNum; i++)
        {
            AVerGetDeviceName(i, wszName);
            WideCharToMultiByte(CP_ACP, 0, wszName, -1, pszName, 260, NULL, NULL);

            ui->AVerList->addItem(pszName);
        }

        // 构建采集卡对象
        if (AVerCreateCaptureObjectEx(0, DEVICETYPE_HD, (HWND)this->ui->win1->winId(), &this->hCaptureDevice1) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Capture SDK Intialize Error");
            return;
        }

        if (AVerCreateCaptureObjectEx(1, DEVICETYPE_HD, (HWND)this->ui->win2->winId(), &this->hCaptureDevice2) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Capture SDK Intialize Error");
            return;
        }
    });
}

void MainWindow::previewInitCtrl()
{
    // 查看第一个设备的视频数据
    connect(this->ui->viewD1, &QPushButton::clicked, this, [=](){

        // 设置适配预览参数参数
        AVerSetVideoSource(this->hCaptureDevice1, VIDEOSOURCE_HDMI);
        AVerSetVideoFormat(this->hCaptureDevice1, VIDEOFORMAT_PAL);
        AVerSetVideoInputFrameRate(this->hCaptureDevice1, 2997);

        // 设置分辨率
        VIDEO_RESOLUTION res;
        res.dwVersion = 1;
        res.dwVideoResolution = VIDEORESOLUTION_1280X720;
        res.bCustom = FALSE;
        res.dwWidth = 1280;
        res.dwHeight = 720;
        if (AVerSetVideoResolutionEx(this->hCaptureDevice1, &res) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Not Supported Framerate");
            return;
        }

        // 预览视频信息
        if (AVerStartStreaming(this->hCaptureDevice1) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Can't StartStreaming");
            return;
        }

//        QMessageBox::information(NULL, "提示", "预览完成！");
    });


    // 预览第二个设备的视频
    connect(this->ui->viewD2, &QPushButton::clicked, this, [=](){

        // 设置适配预览参数参数
        AVerSetVideoSource(this->hCaptureDevice2, VIDEOSOURCE_HDMI);
        AVerSetVideoFormat(this->hCaptureDevice2, VIDEOFORMAT_PAL);
        AVerSetVideoInputFrameRate(this->hCaptureDevice2, 2997);

        // 设置分辨率
        VIDEO_RESOLUTION res;
        res.dwVersion = 1;
        res.dwVideoResolution = VIDEORESOLUTION_1920X1080;
        res.bCustom = FALSE;
        if (AVerSetVideoResolutionEx(this->hCaptureDevice2, &res) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Not Supported Framerate");
            return;
        }

        // 预览视频信息
        if (AVerStartStreaming(this->hCaptureDevice2) != CAP_EC_SUCCESS)
        {
            QMessageBox::information(NULL, "Error", "Can't StartStreaming");
            return;
        }

        QMessageBox::information(NULL, "提示", "预览完成！");
    });
}

void MainWindow::autoWinInitCtrl()
{
    // 设置第一个视频自适应
    connect(this->ui->autoWin1Size,&QPushButton::clicked, this, [=](){

        RECT rect;
        rect.top = 0;
        rect.left = 0;
        rect.right = ui->win1->width();
        rect.bottom = ui->win1->height();
        AVerSetVideoWindowPosition(this->hCaptureDevice1, rect);

    });

    // 设置第二个视频自适应
    connect(this->ui->autoWin2Size,&QPushButton::clicked, this, [=](){

        RECT rect;
        rect.top = 0;
        rect.left = 0;
        rect.right = ui->win2->width();
        rect.bottom = ui->win2->height();
        AVerSetVideoWindowPosition(this->hCaptureDevice2, rect);

    });
}

void MainWindow::recordVideoInitCtrl()
{
    // 开启录制
    connect(this->ui->startRecord,&QPushButton::clicked, this, [=](){

//        VIDEO_CAPTURE_INFO VideoCapInfo = {0};

//        VideoCapInfo.dwSaveType = ST_MP4;
//        VideoCapInfo.dwCaptureType = CT_FILE_MP4;
//        QString filePath = QCoreApplication::applicationDirPath();
//        const char* hehe = filePath.toStdString().c_str();
//        VideoCapInfo.lpFileName = L"D:\\t1.mp4";

        HW_STREAM_CAPTURE_INFO* streamCaptureInfo = new HW_STREAM_CAPTURE_INFO();
        streamCaptureInfo->dwVersion = 1;
        streamCaptureInfo->dwCaptureType = CT_FILE_MP4;
        streamCaptureInfo->lpFileName = (wchar_t *)L"D:\\t1.mp4";
        if (AVerHwCaptureStreamStart(this->hCaptureDevice1, streamCaptureInfo) != CAP_EC_SUCCESS)
        {
             QMessageBox::information(NULL, "tips", "start record device1 failre!");
        }
        streamCaptureInfo->lpFileName = (wchar_t *)L"D:\\t2.mp4";
        if (AVerHwCaptureStreamStart(this->hCaptureDevice2, streamCaptureInfo) != CAP_EC_SUCCESS)
        {
             QMessageBox::information(NULL, "tips", "start record device2 failre!");
        }

    });

    // 停止录制
    connect(this->ui->stopRecord, &QPushButton::clicked, this, [=](){
        AVerHwCaptureStreamStop(this->hCaptureDevice1);
        AVerHwCaptureStreamStop(this->hCaptureDevice2);
    });
}

void MainWindow::layerOverlayInitCtrl()
{

    // 图层叠加测试
    connect(this->ui->layerT1, &QPushButton::clicked, this, [=](){
        QLabel* img = new QLabel();
//        // 直接放置到界面上
        img->setParent(this->ui->groupBox);
        //获取设备屏幕大小
        int lw = 255;
        int lh = 66;
//        img->setText("libel content");
        img->setGeometry(60, 150, lw, lh);
        // 背景
        QPixmap p(":/Res/test.png");
        img->setAlignment(Qt::AlignCenter);
        img->setPixmap(p.scaled(img->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

        img->raise();
        img->show();

    });


    connect(this->ui->layerT2, &QPushButton::clicked, this, [=](){
        QLabel* img = new QLabel(this);
        //获取设备屏幕大小
        int lw = 255;
        int lh = 66;
        img->setGeometry(60, 150, lw, lh);
        // 背景
        QPixmap p(":/Res/test.png");
        img->setAlignment(Qt::AlignCenter);
        img->setPixmap(p.scaled(img->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

        // 透明
        img->setAttribute(Qt::WA_TranslucentBackground, true);
        img->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);

        img->raise();
        img->show();

    });

    connect(this->ui->layerT3, &QPushButton::clicked, this, [=](){
       // TODO 待实现
    });

}
